package cn.fuxi2022.accounts.menu.service.impl;

import cn.fuxi2022.accounts.menu.mapper.AccountsMenuMapper;
import cn.fuxi2022.accounts.menu.service.AccountsMenuService;
import cn.fuxi2022.model.empty.accounts.AccountsMenu;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 微信公众号菜单表 服务实现类
 * </p>
 *
 * @author Fuxi
 * @since 2022-10-18 14:57:39
 */
@Service
public class AccountsMenuServiceImpl extends ServiceImpl<AccountsMenuMapper, AccountsMenu> implements AccountsMenuService {

    @Override
    public List<AccountsMenu> getAllList() {
        // 嵌套JSON实现
        // 查询一级菜单
        LambdaQueryWrapper<AccountsMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(AccountsMenu::getSort).orderByDesc(AccountsMenu::getGmtModified);
        List<AccountsMenu> list = list(wrapper);

        List<AccountsMenu> mapList = list.stream()
                // 获取一级菜单
                .filter(accountsMenu -> "0".equals(accountsMenu.getParentId()))
                // 挂载菜单
                .map(accountsMenu -> covert(accountsMenu, list)).collect(Collectors.toList());
        return mapList;
    }

    /**
     * 挂载分类方法
     *
     * @param accountsMenu 挂载的分类
     * @return 全部分类列表
     */
    public AccountsMenu covert(AccountsMenu accountsMenu, List<AccountsMenu> accountsMenus) {
        List<AccountsMenu> menuList = accountsMenus.stream()
                // 获取当前分类的所有子分类
                .filter(menu -> accountsMenu.getId().equals(menu.getParentId()))
                // 子分类也装载菜单
                .map(accountsMenu1 -> covert(accountsMenu1, accountsMenus)).collect(Collectors.toList());
        accountsMenu.setChildren(menuList);

        return accountsMenu;
    }

}
